<template>
  <div class="warehouse-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>仓库管理</span>
          <el-button type="primary" @click="handleAdd">添加库存</el-button>
        </div>
      </template>
      
      <el-table :data="inventory" style="width: 100%">
        <el-table-column prop="product_name" label="商品名称" />
        <el-table-column prop="quantity" label="数量" />
        <el-table-column prop="location" label="库位" />
        <el-table-column prop="last_updated" label="最后更新" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const inventory = ref([])

async function fetchInventory() {
  try {
    const response = await axios.get('/api/warehouse/inventory/')
    inventory.value = response.data
  } catch (error) {
    console.error('Failed to fetch inventory:', error)
    ElMessage.error('获取库存列表失败')
  }
}

function handleAdd() {
  // TODO: 实现添加库存功能
  ElMessage.info('添加库存功能开发中')
}

function handleEdit(row) {
  // TODO: 实现编辑库存功能
  ElMessage.info('编辑库存功能开发中')
}

async function handleDelete(row) {
  try {
    await ElMessageBox.confirm('确定要删除这条库存记录吗？', '警告', {
      type: 'warning'
    })
    await axios.delete(`/api/warehouse/inventory/${row.id}/`)
    ElMessage.success('删除成功')
    fetchInventory()
  } catch (error) {
    if (error !== 'cancel') {
      console.error('Failed to delete inventory:', error)
      ElMessage.error('删除失败')
    }
  }
}

onMounted(() => {
  fetchInventory()
})
</script>

<style scoped>
.warehouse-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 